package com.mie.controller;

import com.mie.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Mie
 * @Description
 * @Date 2021/11/12 9:01
 **/
@CrossOrigin
@RestController
@RequestMapping("file")
@Api(tags="文件管理")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * @Description 上传文件
     * @Param [file]
     * @return [org.springframework.web.multipart.MultipartFile] 返回UUID
     **/
    @ApiOperation(value = "上传文件")
    @PostMapping("upload")
    public Map uploadFile(MultipartFile file){
        Map map = new HashMap();
        if (file == null){
            System.out.println("空");

        }
        map = fileService.uploadFile(file);
        return map;
    }

    /**
     * @Description 根据uuid获取文件信息
     * @Param [newName] 文件的UUID
     * @return [java.lang.String] 文件元数据
     **/
    @ApiOperation(value = "根据uuid获取文件信息")
    @GetMapping("get/{uuid}")
    public Map getFileData(@PathVariable("uuid") String newName){
        Map map = new HashMap();

        map = fileService.getFileData(newName);

        return map;
    }

    /**
     * @Description  根据uuid下载文件
     * @Param [newName, rs]
     * @return [java.lang.String, javax.servlet.http.HttpServletResponse]
     **/
    @ApiOperation(value = "根据uuid下载文件")
    @GetMapping("download/{uuid}")
    public void downloadFile(@PathVariable("uuid") String newName, HttpServletResponse rs){

        fileService.downloadFile(newName,rs);

    }
}
